package com.dayu.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayu.finecomm.protocol.pojo.TStation;
import com.dayu.finecomm.service.FCService;
import com.dayu.finecomm.source.CollectSource;
import com.dayu.web.param.SensorParam;
import com.dayu.web.param.TokenParam;
import com.dayu.web.pojo.Sensor;
import com.dayu.web.service.StationService;
import com.dayu.web.vo.RESTResponse;

/**
 * 关于站号和位号
 * 
 * @author Chenjiawei
 *
 *         2017年5月24日
 */
@Controller
@RequestMapping("/unitalk/v1.0")
public class StationCtrl {

	@Resource
	FCService finecomm;

	@Resource
	StationService stationService;

	private static Map<String, List<String>> old;

	/**
	 * 获取所有站号信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/stations.json", method = RequestMethod.POST)
	public String getStations(@RequestBody TokenParam token) {

		CollectSource cs = finecomm.getStations();
		Map<String, TStation> map = cs.collection();

		old = stationService.getStations(map);

		return RESTResponse.of(old).toString();
	}

	/**
	 * 获取新站号信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/station/newstation.json", method = RequestMethod.POST)
	public String getNewStations(@RequestBody TokenParam token) {

		CollectSource cs = finecomm.getStations();
		Map<String, TStation> map = cs.collection();

		Map<String, List<String>> curr = stationService.getStations(map);

		if (old == null) {

			if (curr.size() > 0) {
				old = curr;
			}
			return RESTResponse.of(curr).toString();

		} else {
			Map<String, List<String>> ne = stationService.getNewStations(old,
					curr);
			old = stationService.getStations(map);

			return RESTResponse.of(ne).toString();
		}
	}

	/**
	 * 根据该站号获取位号信息
	 * 
	 * @param stationParam
	 *            站号参数
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/station/sensors.json", method = RequestMethod.POST)
	public String getSensors(@RequestBody SensorParam sernsParam) {

		CollectSource cs = finecomm.getStations();
		Map<String, TStation> map = cs.collection();

		List<Sensor> list = stationService.getSensors(sernsParam, map);

		return RESTResponse.of(list).toString();

	}

	/**
	 * 获取客户端总数
	 */
	@ResponseBody
	@RequestMapping(value = "/client.json", method = RequestMethod.POST)
	public String getClientCount(@RequestBody TokenParam token) {

		@SuppressWarnings("static-access")
		List<String> list = finecomm.getClients();

		return RESTResponse.of(list).toString();
	}
}
